<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>菜单管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta http-equiv="Expires" content="0">
		<meta http-equiv="Pragma" content="no-cache">
		<meta http-equiv="Cache-control" content="no-cache">
		<meta http-equiv="Cache" content="no-cache">
		<link rel="stylesheet" href="../../../static/layui-v2.5.6/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../css/app.css" />
	</head>
	<body>
		<fieldset class="layui-elem-field layui-field-title">
			<legend>菜单管理</legend>
		</fieldset>
		<!-- 添加删除加搜索 -->
		<div class="tbr_bar">
			<div class="layui-inline">
				<label class="layui-form-label">上级菜单</label>
				<div class="layui-input-inline">
					<input type="text" placeholder="" id="parentName" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">名称</label>
				<div class="layui-input-inline">
					<input type="text" placeholder="" id="name" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">访问路径</label>
				<div class="layui-input-inline">
					<input type="text" placeholder="" id="uri" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="button" class="layui-btn layui-btn-normal" onclick="getData()">搜索</button>
					<a href="menu_add.html" class="layui-btn layui-btn-normal">添加</a>
					<a href="javascript:;" class="layui-btn layui-btn-normal" id="delsBtn">批量删除</a>
				</div>
			</div>
		</div>
		<table class="layui-hide" id="table" lay-filter="table"></table>

		<script type="text/html" id="barDemo">
			<!-- <a class="layui-btn layui-btn-xs" lay-event="role">分配角色</a> -->
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
		<script src="../../../static/layui-v2.5.6/layui/layui.js" charset="utf-8"></script>
		<script src="../../../js/api.js" charset="utf-8"></script>
		<script src="../../../js/req.js" charset="utf-8"></script>
		<script>
			layui.use(['layer','table'], function() {
				var table = layui.table;
				//
				getData()
				//批量删除
				$("#delsBtn").click(function() {
					var checkStatus = table.checkStatus('idTest');
					var data = checkStatus.data;
					if (data.length === 0) {
						layer.msg("请选择要删除数据");
					} else {
						var checkArr = new Array();
						for(var a = 0;a<data.length;a++){
							checkArr.push(data[a].id);
						}
						layer.confirm('删除后不可恢复,是否继续?', function(index) {
							var url = api.menuDels;
							var parameter = {
								id: checkArr.join(',')
							}; //参数
							let onSuccess = function(data, response) {
								layer.alert(data.msg,function(){
									if (data.code == 200) {										
										getData()
									}
									layer.closeAll();
								});								
							}
							ajax_post(url, parameter, onSuccess);
						});
					}
				})
			})

			function getData() {
				layui.use('table', function() {
					var table = layui.table;

					table.render({
						elem: '#table',
						id:'idTest',
						url: api.menuList,
						method: 'POST',
						limit: 15,
						limits:limits,
						where: {
							token: getToken(),
							parentName:$("#parentName").val(),
							name: $("#name").val(),
							uri: $("#uri").val()
						},
						title: '用户数据表',
						cols: [
							[{
								type: 'checkbox',
								fixed: 'left'
							}, {
								field: 'parentName',
								title: '上级菜单',
								width: 120
							}, {
								field: 'name',
								title: '菜单名称',
								width: 160,
								sort: false
							}, {
								field: 'uri',
								title: '访问路径',
								width: 280
							}, {
								field: 'orderid',
								title: '序号',
								align:'center',
								width: 62
							}, {
								field: 'statu',
								title: '状态',
								align:'center',
								width: 62,templet: function(d){
									if(d.statu === 1){
										return '正常';
									}
									if(d.statu === 2){
										return '暂停使用';
									}
							        return '----';
							    }
							}, {
								field: 'username',
								title: '操作人',
								align:'center',
								width: 92
							}, {
								field: 'updateTime',
								title: '操作时间',
								width: 162,
								align:'center',
								sort: false
							}, {
								fixed: 'right',
								title: '操作',
								width: 120,
								toolbar: '#barDemo'
							}]
						],
						page: true
					});

					//监听行工具事件
					table.on('tool(test)', function(obj) {
						var data = obj.data;
						console.log(data.id)
						if (obj.event === 'del') {
							layer.confirm('删除后不可恢复,是否继续?', function(index) {
								var url = api.menuDel;
								var parameter = {
									id: data.id
								}; //参数
								let onSuccess = function(data, response) {
									layer.alert(data.msg,function(){
										if (data.code == 200) {
											getData();
										}
										layer.closeAll();
									});
									
								}
								ajax_post(url, parameter, onSuccess);
							});
						} else if (obj.event === 'edit') {
							window.location.href = "menu_add.html?id=" + data.id;
						}
					});
				});
			}
		</script>

	</body>
</html>